home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / C⁄C++ OS8 / Everything / EditText.cp < prev    next >
Encoding:
Text File  |  1998-09-06  |  7.7 KB  |  393 lines  |  [TEXT/CWIE]

  1. // EditText.cp
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15.  
  16. #include "Globals.h"
  17. #include "ResourceDefs.h"
  18. #include "DoScrap.h"
  19. #include "Miscellany.h"
  20. #include "Scrolling.h"
  21. #include "ControlUtils.h"
  22. #include "DDocData.h"
  23. #include "EverythingEngine.h"
  24. #include "EverythingDoc.h"
  25.  
  26. #include "EditText.h"
  27.  
  28.  
  29. //----------
  30. void    EditText::Create (
  31.     AMDoc*            inDoc,
  32.     DDocData*        inData)
  33. {
  34.     EditText*        winObj = new EditText;
  35.  
  36.     if (winObj != nil) {
  37.         winObj->Open (inDoc);
  38.         winObj->ConnectToData (inData);
  39.     }
  40. }
  41.  
  42. //----------
  43. EditText::EditText ()
  44. {
  45.     mData = nil;
  46. }
  47.  
  48. //----------
  49. EditText::~EditText ()
  50. {
  51. }
  52.  
  53. //----------
  54. EverythingEngine*    EditText::GetEngine ()
  55. {
  56.     return (EverythingEngine*) mDoc->mEngine;
  57. }
  58.  
  59. //----------
  60. void    EditText::Open (
  61.     AMDoc*            inDoc)
  62. {
  63.     WindowPtr        window;
  64.     Handle            wftb;
  65.  
  66.     mDoc = inDoc;
  67.  
  68.     window = GetNewCWindow (WIND_EditText, nil, (WindowPtr) -1L);
  69.     if (mDoc->mEngine->GetFilename () [0] != 0) {
  70.         SetWTitle (window, mDoc->mEngine->GetFilename ());
  71.     }
  72.     mWindow = window;
  73.     ((EverythingDoc*)mDoc)->mEditTextPtr = window;
  74.  
  75.     SetWindowKind (window, 'AM');
  76.     SetWRefCon (window, (long) this);
  77.     SetPort (window);
  78.     SetInfo (window);
  79.  
  80.     wftb = ::GetResource ('Wftb', WIND_EditText);
  81.  
  82.     CreateRootControl (window, &mRootControl);
  83.  
  84.     vScroll = nil;
  85.     hScroll = nil;
  86.  
  87.  
  88.     mSmallHandle = ::GetNewControl (CNTL_Small, window);
  89.     SetWindowItemFont (mSmallHandle, wftb, 1);
  90.  
  91.     mLargeHandle = ::GetNewControl (CNTL_Large, window);
  92.     SetWindowItemFont (mLargeHandle, wftb, 2);
  93.  
  94.     mX12345Handle = ::GetNewControl (CNTL_X12345, window);
  95.     SetWindowItemFont (mX12345Handle, wftb, 3);
  96.  
  97.     mX12345e6Handle = ::GetNewControl (CNTL_X12345e6, window);
  98.     SetWindowItemFont (mX12345e6Handle, wftb, 4);
  99.  
  100.     mPasswordHandle = ::GetNewControl (CNTL_Password, window);
  101.     SetWindowItemFont (mPasswordHandle, wftb, 5);
  102.  
  103.     mDateHandle = ::GetNewControl (CNTL_Date, window);
  104.     SetWindowItemFont (mDateHandle, wftb, 6);
  105.  
  106.     mTimeHandle = ::GetNewControl (CNTL_Time, window);
  107.     SetWindowItemFont (mTimeHandle, wftb, 7);
  108.  
  109.     mStyledHandle = ::GetNewControl (CNTL_Styled, window);
  110.     SetWindowItemFont (mStyledHandle, wftb, 8);
  111.  
  112.     AdvanceKeyboardFocus (window);
  113.  
  114.     ShowWindow (window);
  115. }
  116.  
  117. //----------
  118. void    EditText::Close ()
  119. {
  120.     mData->RemoveResponder (this);
  121.  
  122.     ((EverythingDoc*)mDoc)->mEditTextPtr = nil;
  123.     SetInfo (nil);
  124.     HideWindow (mWindow);
  125.     DisposeWindow (mWindow);
  126.  
  127.     delete this;
  128. }
  129.  
  130. //----------
  131. void    EditText::ConnectToData (
  132.     DDocData*        inData)
  133. {
  134.     mData = inData;
  135.     mData->AddResponder (this);
  136.  
  137.     SetControlText (mSmallHandle, mData->GetSmall ());
  138.     SetControlText (mLargeHandle, mData->GetLarge ());
  139.     SetControlTextValue (mX12345Handle, mData->GetX12345 ());
  140.     SetControlTextFloat (mX12345e6Handle, mData->GetX12345e6 ());
  141.     SetControlText (mPasswordHandle, mData->GetPassword ());
  142.     SetClockDateTime (mDateHandle, mData->GetDate ());
  143.     SetClockDateTime (mTimeHandle, mData->GetTime ());
  144.     SetControlText (mStyledHandle, mData->GetStyled ());
  145. }
  146.  
  147. //----------
  148. void    EditText::DataChanged (
  149.     long        inDataID)
  150. {
  151.     if (inDataID == idSmall) {
  152.         SetControlText (mSmallHandle, mData->GetSmall ());
  153.     }
  154.     if (inDataID == idLarge) {
  155.         SetControlText (mLargeHandle, mData->GetLarge ());
  156.     }
  157.     if (inDataID == idX12345) {
  158.         SetControlTextValue (mX12345Handle, mData->GetX12345 ());
  159.     }
  160.     if (inDataID == idX12345e6) {
  161.         SetControlTextFloat (mX12345e6Handle, mData->GetX12345e6 ());
  162.     }
  163.     if (inDataID == idPassword) {
  164.         SetControlText (mPasswordHandle, mData->GetPassword ());
  165.     }
  166.     if (inDataID == idDate) {
  167.         SetClockDateTime (mDateHandle, mData->GetDate ());
  168.     }
  169.     if (inDataID == idTime) {
  170.         SetClockDateTime (mTimeHandle, mData->GetTime ());
  171.     }
  172.     if (inDataID == idStyled) {
  173.         SetControlText (mStyledHandle, mData->GetStyled ());
  174.     }
  175. }
  176.  
  177. //----------
  178. void    EditText::Control (
  179.     ControlHandle        whichControl,
  180.     short                whichPart,
  181.     Point                where)
  182. {
  183.     Rect            bounds;
  184.     short            newValue;
  185.  
  186.     ExitCurField ();
  187.  
  188.     if (whichControl == mSmallHandle) {
  189.         HandleEditClick (mSmallHandle, where);
  190.     }
  191.     if (whichControl == mLargeHandle) {
  192.         HandleEditClick (mLargeHandle, where);
  193.     }
  194.     if (whichControl == mX12345Handle) {
  195.         HandleEditClick (mX12345Handle, where);
  196.     }
  197.     if (whichControl == mX12345e6Handle) {
  198.         HandleEditClick (mX12345e6Handle, where);
  199.     }
  200.     if (whichControl == mPasswordHandle) {
  201.         HandleEditClick (mPasswordHandle, where);
  202.     }
  203.     if (whichControl == mDateHandle) {
  204.         HandleEditClick (mDateHandle, where);
  205.     }
  206.     if (whichControl == mTimeHandle) {
  207.         HandleEditClick (mTimeHandle, where);
  208.     }
  209.     if (whichControl == mStyledHandle) {
  210.         HandleEditClick (mStyledHandle, where);
  211.     }
  212. }
  213.  
  214. //----------
  215. void    EditText::MouseIn (
  216.     Point        where,
  217.     short        modifiers)
  218. {
  219.     Rect        bounds;
  220.  
  221. }
  222.  
  223. //----------
  224. void    EditText::ExitCurField ()
  225. {
  226.     ControlHandle    focus;
  227.  
  228.     GetKeyboardFocus (mWindow, &focus);
  229.  
  230.     if (focus == nil) {
  231.         // nothing to exit
  232.  
  233.     } else if (focus == mSmallHandle) {
  234.         mData->SetSmall (GetEditTextChars (mSmallHandle));
  235.     } else if (focus == mLargeHandle) {
  236.         mData->SetLarge (GetEditTextChars (mLargeHandle));
  237.     } else if (focus == mX12345Handle) {
  238.         mData->SetX12345 (GetControlTextValue (mX12345Handle));
  239.     } else if (focus == mX12345e6Handle) {
  240.         mData->SetX12345e6 (GetControlTextFloat (mX12345e6Handle));
  241.     } else if (focus == mPasswordHandle) {
  242.         mData->SetPassword (GetEditTextChars (mPasswordHandle));
  243.     } else if (focus == mDateHandle) {
  244.         mData->SetDate (GetClockDateTime (mDateHandle));
  245.     } else if (focus == mTimeHandle) {
  246.         mData->SetTime (GetClockDateTime (mTimeHandle));
  247.     } else if (focus == mStyledHandle) {
  248.         mData->SetStyled (GetEditTextChars (mStyledHandle));
  249.     }
  250. }
  251.  
  252. //----------
  253. void    EditText::TypeIn (
  254.     char        ch)
  255. {
  256.     ControlHandle    focus;
  257.     SInt16            keyCode;
  258.  
  259.     GetKeyboardFocus (mWindow, &focus);
  260.  
  261.     if ((ch == charEnter)
  262.     ||  (ch == charReturn)) {
  263.         ExitCurField ();
  264.     } else if (ch == charEsc) {
  265.     } else if (ch == charTab) {
  266.         DoTab ((curEvent.modifiers & optionKey) != 0);
  267.     } else if (focus != nil) {
  268.         keyCode = (SInt16)(curEvent.message & keyCodeMask);
  269.         HandleControlKey (focus, keyCode, ch, (SInt16)curEvent.modifiers);
  270.         mDoc->mEngine->SetDirty ();
  271.     } else {
  272.         SysBeep (1);
  273.     }
  274. }
  275.  
  276. //----------
  277. void    EditText::Resize ()
  278. {
  279.     /* application-specific code to resize items in window */
  280. }
  281.  
  282. //----------
  283. void    EditText::Scroll (
  284.     short        newValue,
  285.     short        oldValue)
  286. {
  287.     /* application-specific code to scroll window */
  288.     if (gWhichScroll == vScroll) {
  289.     } else {    // horizontal
  290.     }
  291. }
  292.  
  293. //----------
  294. void    EditText::DoUndo ()
  295. {
  296. } // DoUndo
  297.  
  298. //----------
  299. void    EditText::DoCut ()
  300. {
  301.     TEHandle        curTE = GetCurTE ();
  302.  
  303.     if (curTE != nil) {
  304.         TECut (curTE);
  305.         mDoc->mEngine->SetDirty ();
  306.         scrapDirty = true;
  307.     }
  308. } // DoCut
  309.  
  310. //----------
  311. void    EditText::DoCopy ()
  312. {
  313.     TEHandle        curTE = GetCurTE ();
  314.  
  315.     if (curTE != nil) {
  316.         TECopy (curTE);
  317.         scrapDirty = true;
  318.     }
  319. } // DoCopy
  320.  
  321. //----------
  322. void    EditText::DoPaste ()
  323. {
  324.     TEHandle        curTE = GetCurTE ();
  325.  
  326.     if (curTE != nil) {
  327.         TEPaste (curTE);
  328.         mDoc->mEngine->SetDirty ();
  329.     }
  330. } // DoPaste
  331.  
  332. //----------
  333. void    EditText::DoClear ()
  334. {
  335.     TEHandle        curTE = GetCurTE ();
  336.  
  337.     if (curTE != nil) {
  338.         TEDelete (curTE);
  339.         mDoc->mEngine->SetDirty ();
  340.     }
  341. } // DoClear
  342.  
  343. //----------
  344. void    EditText::DoSelectAll ()
  345. {
  346.     TEHandle        curTE = GetCurTE ();
  347.  
  348.     if (curTE != nil) {
  349.         TESetSelect (0, 32767, curTE);
  350.     }
  351. } // DoSelectAll
  352.  
  353. //----------
  354. void    EditText::DoShowClipboard ()
  355. {
  356. } // DoShowClipboard
  357.  
  358. //----------
  359. Boolean        EditText::DoCommand (
  360.     long        inCommand)
  361. {
  362.     Boolean        result = true;
  363.  
  364.     switch (inCommand) {
  365.         case cmdUndo:
  366.                 DoUndo ();
  367.             break;
  368.         case cmdCut:
  369.                 DoCut ();
  370.             break;
  371.         case cmdCopy:
  372.                 DoCopy ();
  373.             break;
  374.         case cmdPaste:
  375.                 DoPaste ();
  376.             break;
  377.         case cmdClear:
  378.                 DoClear ();
  379.             break;
  380.         case cmdSelectAll:
  381.                 DoSelectAll ();
  382.             break;
  383.         case cmdShowClipboard:
  384.                 DoShowClipboard ();
  385.             break;
  386.  
  387.         default:
  388.                 result = false;
  389.     } // switch
  390.  
  391.     return result;
  392. }
  393.